/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */

function testRectController (editor) {
  var element = editor.getSVGDocument().createElement("rect");
  editor.getSVGDocument().appendTemporaryElement(element);
  try {
    // test if we can set its size and position absolutely   
    var controller = getSVGElementControllerVault().getElementControllerFor(element);
    controller.moveTo(element, 100, 200);				 
    controller.resizeTo(element, 300, 400);		  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 100);
    this.assertEquals(bbox.y, 200);
    this.assertEquals(bbox.width, 300);
    this.assertEquals(bbox.height, 400);
  
    // test if we can set its size and position relatively   
  
    controller.moveBy(element, 100, 200);
    controller.resizeBy(element, 2, 0.5);	  
  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 200);
    this.assertEquals(bbox.y, 400);
    this.assertEquals(bbox.width, 600);
    this.assertEquals(bbox.height, 200);
  
    // TODO: test if we can rotate it	 
  } finally {
    editor.getSVGDocument().removeTemporaryElement(element);	
  }
};

function testEllipseController (editor) {
  var element = editor.getSVGDocument().createElement("ellipse");
  editor.getSVGDocument().appendTemporaryElement(element);
  try {
    // test if we can set its size and position absolutely   
    var controller = getSVGElementControllerVault().getElementControllerFor(element);
    controller.moveTo(element, 100, 200);				 
    controller.resizeTo(element, 300, 400);		  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 100);
    this.assertEquals(bbox.y, 200);
    this.assertEquals(bbox.width, 300);
    this.assertEquals(bbox.height, 400);
  
    // test if we can set its size and position relatively   
  
    controller.moveBy(element, 100, 200);
    controller.resizeBy(element, 2, 0.5);	  
  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 200);
    this.assertEquals(bbox.y, 400);
    this.assertEquals(bbox.width, 600);
    this.assertEquals(bbox.height, 200);
  
    // TODO: test if we can rotate it	 
  } finally {
    editor.getSVGDocument().removeTemporaryElement(element);	
  }
};

function testPathController (editor) {
  var element = editor.getSVGDocument().createElement("path");
  
  editor.getSVGDocument().appendTemporaryElement(element);
  try {
    var controller = getSVGElementControllerVault().getElementControllerFor(element);
	// first we need to draw it
	
    controller.appendSegment(element, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('M', true, [{ x: 250, y: 150 }]));
	controller.appendSegment(element, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{ x: 150, y: 350 }]));
	controller.appendSegment(element, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{ x: 350, y: 350 }]));
	controller.appendSegment(element, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('Z', true, []));
	controller.updatePath(element);
		
	// test if we can set its size and position absolutely       
    controller.moveTo(element, 100, 200);				 
    controller.resizeTo(element, 300, 400);		  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 100);
    this.assertEquals(bbox.y, 200);
    this.assertEquals(bbox.width, 300);
    this.assertEquals(bbox.height, 400);
  
    // test if we can set its size and position relatively   
  
    controller.moveBy(element, 100, 200);
    controller.resizeBy(element, 2, 0.5);	  
  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 200);
    this.assertEquals(bbox.y, 400);
    this.assertEquals(bbox.width, 600);
    this.assertEquals(bbox.height, 200);
  
    // TODO: test if we can rotate it	 
  } finally {
    editor.getSVGDocument().removeTemporaryElement(element);	
  }
};

function testLineController (editor) {
  var element = editor.getSVGDocument().createElement("line");
  editor.getSVGDocument().appendTemporaryElement(element);
  try {
    // test if we can set its size and position absolutely   
    var controller = getSVGElementControllerVault().getElementControllerFor(element);
    controller.moveTo(element, 100, 200);				 
    controller.resizeTo(element, 300, 400);		  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 100);
    this.assertEquals(bbox.y, 200);
    this.assertEquals(bbox.width, 300);
    this.assertEquals(bbox.height, 400);
  
    // test if we can set its size and position relatively   
  
    controller.moveBy(element, 100, 200);
    controller.resizeBy(element, 2, 0.5);	  
  
    var bbox = element.getBBox();
    this.assertEquals(bbox.x, 200);
    this.assertEquals(bbox.y, 400);
    this.assertEquals(bbox.width, 600);
    this.assertEquals(bbox.height, 200);
  
    // TODO: test if we can rotate it	 
  } finally {
    editor.getSVGDocument().removeTemporaryElement(element);	
  }
};

function testGroupController (editor) {
  var gElement = editor.getSVGDocument().createElement("g");
  
  editor.getSVGDocument().appendTemporaryElement(gElement);
  try {
    var pathElement = editor.getSVGDocument().createElement("path");
	var pathController = getSVGElementControllerVault().getElementControllerFor(pathElement);
    pathController.appendSegment(pathElement, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('M', true, [{ x: 25, y: 50 }]));
	pathController.appendSegment(pathElement, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{ x: 37.5, y: 25 }]));
	pathController.appendSegment(pathElement, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('L', true, [{ x: 50, y: 50 }]));
	pathController.appendSegment(pathElement, new fi.foyt.svg.svgdom.FNISVGEditorPathElementSegment('Z', true, []));
	pathController.updatePath(pathElement);
	gElement.appendChild(pathElement);
	
	var rectElement = editor.getSVGDocument().createElement("rect");
	var rectController = getSVGElementControllerVault().getElementControllerFor(rectElement);
    rectController.resizeTo(rectElement, 25, 25);
	rectController.moveTo(rectElement, 100, 100);
	gElement.appendChild(rectElement);
	
	var controller = getSVGElementControllerVault().getElementControllerFor(gElement);
	var bbox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(gElement);
	this.assertEquals([bbox.x, bbox.y, bbox.width, bbox.height], [25, 25, 100, 100], "initial values");
	
	// test if we can set its size and position absolutely
	
	controller.moveTo(gElement, 50, 50);		 
	controller.resizeTo(gElement, 100, 100);	  
    bbox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(gElement);
	
	this.assertEquals([Math.round(bbox.x), Math.round(bbox.y), Math.round(bbox.width), Math.round(bbox.height)], [50, 50, 100, 100], "move & resize (absolute)");

    // test if we can set its size and position relatively   
  
    controller.moveBy(gElement, 25, 25);    
	controller.resizeBy(gElement, 3, 3);	  
  
    bbox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(gElement);
	this.assertEquals([Math.round(bbox.x), Math.round(bbox.y), Math.round(bbox.width), Math.round(bbox.height)], [75, 75, 300, 300], "move&resize (relative)");
	
    controller.moveBy(gElement, 25, 25);    
  
    bbox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(gElement);
	this.assertEquals([Math.round(bbox.x), Math.round(bbox.y), Math.round(bbox.width), Math.round(bbox.height)], [100, 100, 300, 300], "move&resize (relative)");

    // TODO: test if we can rotate it	 
  } finally {
    editor.getSVGDocument().removeTemporaryElement(gElement);	
  }
};

FNISVGEditorTestsPlugin = Class.create(FNISVGEditorPlugin, {
  initialize: function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);    
	    
	var button = new FNIPushButton({});	
	button.domNode.addClassName('fniSVGEditor_TestsPlugin_Button');	
	button.addListener('buttonClick', this, this._onButtonClick);
  button.setTooltip(SVGLocale.getText("testsPlugin_buttonTooltip"));
	editorInstance.registerToolbarItem('runUnitTests', button);
  },
  getName: function () {
    return "tests";
  },
  assertEquals: function (v1, v2, name) {
    var match = true;
	
	if (Object.isArray(v1)) {	  
	  for (var i = 0; i < v1.length; i++) {
	  	if (v1[i] != v2[i])
		  match = false;
	  } 
	} else 
	  match = v1 == v2;
	
	if (match != true)
  	  throw new Error(((name != undefined) ? '(' + name + ') ': '') + v1 + " != " + v2);
  },  
  runTest: function (testFunction) {
  	var result = false;
	
	try {
	  eval(testFunction).call(this, this.getEditorInstance());
	  result = true;	  
	} catch (e) {	  
	  addDebugText('<span style="color:#ff0000"> ' + testFunction + ':' + e + '</span>');	  
	}
	
	if (result == true)
	  addDebugText('<span style="color:#00ff00"> ' + testFunction + ': ok' + '</span>');
  },
  _onButtonClick: function (event) {
  	this.runTest("testRectController");
	this.runTest("testEllipseController");	  
	this.runTest("testPathController");
	this.runTest("testLineController");	
	this.runTest("testGroupController");	
  }    
});

document.fire("svgEditorPluginLoaded:tests", { pluginClass: FNISVGEditorTestsPlugin });
